package com.redsoft.spirit.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.redsoft.spirit.entity.JsonResult;
import com.redsoft.spirit.entity.Role;
import com.redsoft.spirit.service.AccountService;
import com.redsoft.spirit.service.RoleService;

@RestController
public class RoleController {

	@Autowired
	private RoleService roleService;

	@Autowired
	private AccountService accountService;

	private Gson gson = new Gson();

	/**
	 * 增加或者修改角色
	 * 
	 * <pre>
	 * 增加或者修改角色
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param menu
	 *            角色实体类
	 * @return
	 */
	@RequestMapping(value = "/roles", method = RequestMethod.POST)
	public String addRole(@RequestBody Role role) {
		try {
			roleService.saveOrUpdateRole(role);
			return new JsonResult().success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 分页查询角色
	 * 
	 * <pre>
	 * 分页查询角色
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param offset
	 * @param limit
	 * @param searchKey
	 * @param userId
	 * @param deptId
	 * @return
	 */
	@RequestMapping(value = "/roles", method = RequestMethod.GET)
	public String roleList(int offset, int limit, String searchKey, String userId, String deptId) {
		JsonResult result = new JsonResult();
		try {
			HashMap map = roleService.getJsonData(offset, limit, searchKey, userId, deptId);
			result.setData(map);
			return result.success().toString("yyyy-MM-dd HH:mm:ss");
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			return gson.toJson(result.failure());
		}
	}

	/**
	 * 删除一个角色
	 * 
	 * <pre>
	 * 删除一个角色
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param menu
	 *            角色实体类
	 * @return
	 */
	@RequestMapping(value = "/roles/{ids}", method = RequestMethod.DELETE)
	public String deleteRole(@PathVariable String ids) {
		try {
			String[] roles = ids.split(",");
			List<String> list = java.util.Arrays.asList(roles);
			roleService.delete(list);
			return new JsonResult().success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 分配菜单时候的 查询角色list
	 * 
	 * <pre>
	 * 	分配菜单时候的 查询角色list
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@RequestMapping(value = "/roles/list", method = RequestMethod.GET)
	public String roleList() {
		JsonResult result = new JsonResult();
		try {
			List<Role> list = roleService.getRoleList();
			result.setData(list);
			return result.success().toString();
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			return gson.toJson(result.failure());
		}
	}

	/**
	 * 根据角色查询已经存在user
	 * 
	 * <pre>
	 * 根据角色查询user,带分页功能
	 * </pre>
	 *
	 * @author 杨雷
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value = "/roles/{roleId}/users", method = RequestMethod.GET)
	public String userListByRole(@PathVariable String roleId, int offset, int limit, String searchKey) {
		JsonResult result = new JsonResult();
		try {
			Map map = accountService.getJsonDataByRole(roleId, offset, limit, searchKey);
			result.setData(map);
			return result.success().toString();
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			return gson.toJson(result.failure());
		}
	}

	/**
	 * 根据角色,关键词查询不在此角色下的用户
	 * 
	 * <pre>
	 * 	根据角色,关键词查询不在此角色下的用户,查询的是后台登录的用户
	 * </pre>
	 *
	 * @author 杨雷
	 * @param roleId
	 * @param searchKey
	 * @param deptId
	 * @return
	 */
	@RequestMapping(value = "/roles/{roleId}/users/out", method = RequestMethod.GET)
	public String userListByRole(@PathVariable String roleId, String searchKey, String deptId) {
		JsonResult result = new JsonResult();
		try {
			List list = accountService.getUserListByOutRole(roleId, searchKey, deptId);
			result.setData(list);
			return result.success().toString();
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			return gson.toJson(result.failure());
		}
	}

	/**
	 * 在某个角色下添加用户.
	 * 
	 * <pre>
	 * 在某个角色下添加用户
	 * </pre>
	 *
	 * @author 杨雷
	 * @param roleId
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/roles/{roleId}/users", method = RequestMethod.POST)
	public String addRoleUser(@PathVariable String roleId, String userId) {
		JsonResult result = new JsonResult();
		try {
			roleService.addRoleUser(roleId, userId);
			return result.success().toString();
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			return gson.toJson(result.failure());
		}
	}

	/**
	 * 根据角色,关键词查询不在此角色下的用户.
	 * 
	 * <pre>
	 * 根据角色,关键词查询不在此角色下的用户
	 * </pre>
	 *
	 * @author 杨雷
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value = "/roles/{roleId}/users/{userId}", method = RequestMethod.DELETE)
	public String deleteRoleUser(@PathVariable String roleId, @PathVariable String userId) {
		JsonResult result = new JsonResult();
		try {
			roleService.deleteRoleUser(roleId, userId);
			return result.success().toString();
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			return gson.toJson(result.failure());
		}
	}

	/**
	 * 保存某个角色下的分配的菜单
	 * 
	 * <pre>
	 * 保存某个角色下的分配的菜单
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param roleId
	 * @param menuIds
	 * @param indeterminateIds
	 * @return
	 */
	@PostMapping("/roles/{roleId}/menus")
	public String saveMenuWithRole(@PathVariable String roleId, String menuIds, String indeterminateIds) {
		roleService.saveRoleMenu(roleId, menuIds, indeterminateIds);
		return new JsonResult().success().toString();
	}
}